package com.saasitems.admin.web.controller;

import com.saasitems.admin.model.PartsType;
import com.saasitems.admin.model.Product;
import com.saasitems.admin.service.ProductService;
import com.saasitems.admin.web.vo.product.*;
import com.saasitems.common.model.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/15.
 */
@Controller
public class ProductController extends BaseController {

    @Autowired
    private ProductService productService;

    @RequestMapping(value="admin/product/index",method= RequestMethod.GET)
    public ModelAndView index() {
        PageInfo page = new PageInfo(1, 1);
        List<ProductVO> list = productService.query(null, page);
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("info", list);
        model.put("queryvo", new ProductQueryVO());
        model.put("page", page);
        return new ModelAndView("admin/product/index", model);
    }

    @RequestMapping(value="admin/product/index",method= RequestMethod.POST)
    public ModelAndView index(ProductQueryVO queryVO, PageInfo page) {
        List<ProductVO> list = productService.query(queryVO, page);
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("info", list);
        model.put("queryvo", queryVO);
        model.put("page", page);
        return new ModelAndView("admin/product/index", model);
    }

    @RequestMapping(value="admin/product/add",method= RequestMethod.GET)
    public ModelAndView add() {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("info", new Product());

        return new ModelAndView("admin/product/add", model);
    }

    @RequestMapping(value="admin/product/add",method= RequestMethod.POST)
    @ResponseBody
    /**
     * @param pics 提交的图片列
     */
    public Object add(Product vo, UpFiles fs, HttpServletRequest request) {

        if (vo != null) {
            vo.setCreUserId(0L);
            vo.setCreDateTime(new Date());
            vo.setUpdUserId(0L);
            vo.setUpdDateTime(new Date());
            productService.insert(vo, fs);
        }
        logger.info("添加成功");
        // return new ModelAndView("admin/product/add", model);
        return this.renderSuccess("添加成功", this.convertUrl(request, "admin/product/index.shtml"));
    }

    @RequestMapping(value="admin/product/test",method= RequestMethod.GET)
    public ModelAndView test() {
        return new ModelAndView("admin/product/test");
    }

    @RequestMapping(value="admin/product/edit/{id}",method= RequestMethod.GET)
    public ModelAndView edit(@PathVariable("id") Long id) {
        Map<String, Object> model = new HashMap<String, Object>();

        if (id != null && !id.equals(0)) {
            ProductVO vo = this.productService.getInfoById(id);
            model.put("info", vo);
        }

        UpFiles ups = this.productService.getFilesByProductId(id);
        model.put("files", ups);
//        if (id != null && !id.equals(0)) {
//            PartsType pt = partsTypeService.getInfo(id);
//            model.put("info", pt);
//        } else {
//            model.put("info", new PartsType());
//        }
        return new ModelAndView("admin/product/edit", model);
    }

    @RequestMapping(value="admin/product/edit",method= RequestMethod.POST)
    @ResponseBody
    public Object edit(Product vo, UpFiles fs, HttpServletRequest request) {

        if (vo != null) {
//            vo.setCreUserId(0L);
//            vo.setCreDateTime(new Date());
            vo.setUpdUserId(0L);
            vo.setUpdDateTime(new Date());
            productService.update(vo, fs);
        }
        logger.info("更新成功");
        return this.renderSuccess("更新成功", this.convertUrl(request, "admin/product/index.shtml"));
    }
}
